differ
Dictionary Differ.
Attributes⚓︎
Classes⚓︎
DiffResults ⚓︎
Bases: BaseModel
Result from calculating the diff.
Source code in pytest_cache_assert/_check_assert/differ.py
class DiffResults(BaseModel):
"""Result from calculating the diff."""
results: Dict # type: ignore[type-arg]
@beartype
def to_dict(self) -> Dict: # type: ignore[type-arg]
return self.results
@beartype
def append(self, assert_rule: AssertRule, result: Dict) -> None: # type: ignore[type-arg]
self.results[f'For {assert_rule}'] = result
Functions⚓︎
diff_with_rules ⚓︎
diff_with_rules(*, old_dict, new_dict, assert_rules)
Determine the differences between two dictionaries.
| PARAMETER | DESCRIPTION |
|---|---|
old_dict |
old dictionary (typically cached one)
TYPE:
|
new_dict |
new dictionary (typically test data)
TYPE:
|
assert_rules |
list of assert rules to ignore certain differences
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DiffResults
|
Diff Object
TYPE:
|
Source code in pytest_cache_assert/_check_assert/differ.py
@beartype
def diff_with_rules(*, old_dict: T_DIFF, new_dict: T_DIFF, assert_rules: List[AssertRule]) -> DiffResults:
"""Determine the differences between two dictionaries.
Args:
old_dict: old dictionary (typically cached one)
new_dict: new dictionary (typically test data)
assert_rules: list of assert rules to ignore certain differences
Returns:
DiffResults: Diff Object
"""
key_str = 'str'
key_re = 'regex'
collector: Dict[str, List[Union[str, Pattern[str]]]] = { # noqa: TAE002
key_str: [],
key_re: [],
}
for ar in assert_rules:
collector[key_re if ar.is_regex() else key_str].append(ar.pattern)
diff_result = _raw_diff(
old_dict=old_dict,
new_dict=new_dict,
exclude_paths=collector[key_str],
exclude_regex_paths=collector[key_re],
)
for ar in assert_rules:
paths = []
for data_set in [old_dict, new_dict]:
ds = DeepSearch(data_set, ar.pattern, use_regexp=ar.is_regex())
paths.extend([*ds.get('matched_paths', {})])
for pth in set(paths):
new_value, old_value = NotFound(), NotFound()
with suppress(KeyError):
old_value = extract(old_dict, pth)
with suppress(KeyError):
new_value = extract(new_dict, pth)
if not ar.func(old_value, new_value):
diff_result.append(ar, {'old_value': old_value, 'new_value': new_value})
return diff_result
Last update:
August 30, 2023
Created: August 30, 2023
Created: August 30, 2023